/*
 * CallEventHandler.java
 *
 * Copyright(c) IronOne Technologies (Pvt) Ltd.  All Rights Reserved.
 * This software is the proprietary information of IronOne Technologies (Pvt) Ltd.
 *
 */
package com.ironone.streaming.evt;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import com.ironone.streaming.R;
import com.ironone.streaming.application.CusAlrtBtnTypes;
import com.ironone.streaming.application.CusAlrtIconTypes;
import com.ironone.streaming.application.CustomAlertDialog;
import com.ironone.streaming.mediaplayer.MediaPlayerStatus;
import com.ironone.streaming.mediaplayer.MyMediaPlayer;
import com.ironone.streaming.mediaplayer.MyMediaPlayerServiceNotifyStatus;

/**
 * {This class h}
 * @Created on May 3, 2013 : 9:48:56 AM
 * @author amilag @author DHANUSHKAK
 *
 */
public class CallEventHandler{
	private static CallEventHandler callhandler;
	private PhoneStateListener phoneStateListener;
	Context con1;
	PhoneStateListener listener;
	String djNumber;
	CustomAlertDialog alertDialog;
	
	private CallEventHandler() {
	}

	public static CallEventHandler getInsatance(){
		if (callhandler == null) {
			callhandler = new CallEventHandler();
		}
		return callhandler;
	}

	/*public void onResumeActivity(Activity act) {
		act.registerReceiver(UIStateManager.getInsatance(), new IntentFilter("stopped"));

		act.registerReceiver(UIStateManager.getInsatance(), new IntentFilter("playing"));

		TelephonyManager telephonyManager = (TelephonyManager) act.getSystemService(Activity.TELEPHONY_SERVICE);
		if (telephonyManager != null) {
			telephonyManager.listen(CallEventHandler.getInsatance()
					.getPhoneStateListenerObject(),
					PhoneStateListener.LISTEN_CALL_STATE);
		}
	}

	public void getcon(Context con){
		this.con=con;
	}*/

	public PhoneStateListener getPhoneStateListenerObject(){
		phoneStateListener = new PhoneStateListener() {
			@Override
			public void onCallStateChanged(int state, String incomingNumber) {
				super.onCallStateChanged(state, incomingNumber);
				if (state == TelephonyManager.CALL_STATE_RINGING) {
				} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
				}
			}
		};
		return phoneStateListener;
	}

	public void callAlert(final Context con){
		
		if(!(con instanceof Activity))
			return;
		
		alertDialog = new CustomAlertDialog((Activity)con, CusAlrtBtnTypes.ACCEPT_DECLINE, CusAlrtIconTypes.CALL);
		alertDialog.setTitleText(con.getString(R.string.authorize_connection));
		alertDialog.setOkButtonClickEvent().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				callUsNow(con);
				alertDialog.dismiss();
			}
		});
		alertDialog.show();
		
		/*AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(con);
		// set title
<<<<<<< .mine
		alertDialogBuilder.setTitle("Are you sure?");
=======
		alertDialogBuilder.setTitle(R.string.are_you_sure_);
>>>>>>> .r59
		// set dialog message

		alertDialogBuilder
<<<<<<< .mine
		/*.setMessage(djNumber)*/
		.setCancelable(false)
		.setPositiveButton("Cancel",new DialogInterface.OnClickListener() {
=======
		/*.setMessage(djNumber)*/
		/*.setCancelable(false)
		.setPositiveButton(R.string.cancel,new DialogInterface.OnClickListener() {
>>>>>>> .r59
			public void onClick(DialogInterface dialog,int id) {
				dialog.cancel();
				/*if(RadioPlayer.getInstance().getMediaPlayer().isPlaying()){
					Intent intent = new Intent();
					intent.setAction("playing");
					con.sendBroadcast(intent);
				}*/
			/*}
		})
		.setNegativeButton(R.string.call,new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,int id) {
				//Intent intent = new Intent();
				//intent.setAction("stopped");
				//con.sendBroadcast(intent);
				callUsNow(con);
			}
		});*/

		// create alert dialog
		//AlertDialog alertDialog = alertDialogBuilder.create();
		// show it
		//if(RadioPlayer.getInstance().getMediaPlayer().isPlaying()){
			//Intent intent = new Intent();
			//intent.setAction("playing");
			//con.sendBroadcast(intent);
		//}
		//alertDialog.show();
		// TextView messageText = (TextView)alertDialog.findViewById(android.R.id.message);
		// messageText.setGravity(Gravity.CENTER);
	}

	public void setDjNum(String number){
		djNumber = number;
	}

	public void callUsNow(Context con) {
		Intent intent1 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
		intent1.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_PLAYER.ordinal());
		con.sendBroadcast(intent1);
		MyMediaPlayer.setMediaPlayerStatus(MediaPlayerStatus.RESETED);
		//MyMediaPlayer.setMediaPlayerStatus(MediaPlayerStatus.PAUSED);
		
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		String djnum = "tel:" + djNumber;
		callIntent.setData(Uri.parse(djnum));
		con.startActivity(callIntent);
	}
}